package com.zf.zffencheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zf.zffencheng.dao.HardwarePartnerDao;
import com.zf.zffencheng.exception.ResultException;
import com.zf.zffencheng.model.dto.HardwarePartnerFilter;
import com.zf.zffencheng.model.pojo.HardwarePartner;
import com.zf.zffencheng.service.HardwarePartnerService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: HardwarePartnerServiceImpl <br/>
 * Description:  <br/>
 * date: 2021/5/28 16:00<br/>
 *
 * @author william <br/>
 */
@Service
public class HardwarePartnerServiceImpl extends ServiceImpl<HardwarePartnerDao, HardwarePartner> implements HardwarePartnerService {
    @Override
    public List<HardwarePartner> listFilter(HardwarePartnerFilter hardwarePartnerFilter) {

        QueryWrapper<HardwarePartner> queryWrapper = new QueryWrapper<>();
        if(hardwarePartnerFilter != null){
            if(hardwarePartnerFilter.getTypeId() != null){
                queryWrapper.eq("typeId", hardwarePartnerFilter.getTypeId());
            }

            if(hardwarePartnerFilter.isNewVersionSort()){
                queryWrapper.orderByDesc("Version");
            }
        }

        return list(queryWrapper);
    }

    @Override
    public HardwarePartner getAndVaild(Integer apiId) {
        if (apiId == null || apiId == 0) {
            throw new ResultException(404, "设备服务商id不存在");
        }
        HardwarePartner hardwarePartnerDTO = getById(apiId);
        if (hardwarePartnerDTO == null) {
            throw new ResultException(404, "未找到服务商");
        }
        return hardwarePartnerDTO;
    }
}
